<template>
  <!-- 用户信息 -->
  <div class="info-right">
    <div class="info-icon">
      <span class="el-icon-bell"></span>
      <span class="el-icon-chat-line-round"></span>
      <span class="el-icon-s-home"></span>
    </div>

    <el-dropdown>
      <span class="el-dropdown-link">
        <el-avatar
          size="small"
          src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
        ></el-avatar>
        <span class="name">{{ userInfo.name }}</span>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item icon="el-icon-switch-button" @click="handleExitBtn"
            >退出登录</el-dropdown-item
          >
          <el-dropdown-item
            divided
            icon="el-icon-user"
            @click="handleUserInfoBtn"
            >用户信息</el-dropdown-item
          >

          <el-dropdown-item icon="el-icon-lock">系统管理</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>

    <!-- 用户信息dialog -->
    <el-dialog title="用户信息" v-model="dialogVisible" width="30%" center>
      <div class="userInfo">
        <span
          >用户名：<strong>{{ userInfo.name }}</strong></span
        >
        <span
          >电话号码：<strong>{{ userInfo.cellphone }}</strong></span
        >
        <span
          >所属部门：<strong>{{ userInfo.department.name }}</strong></span
        >
        <span
          >角色：<strong>{{ userInfo.role.name }}</strong></span
        >
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'
import localCache from '@/utils/cache'

export default defineComponent({
  setup() {
    // nav-header右边的用户名
    const store = useStore()
    const userInfo = computed(() => store.state.login.userInfo)

    // 退出登录按钮逻辑
    const router = useRouter()
    const handleExitBtn = () => {
      localCache.deleteCache('token')
      router.push('/main')
    }

    // 用户信息按钮逻辑
    const dialogVisible = ref(false)
    const handleUserInfoBtn = () => {
      dialogVisible.value = true
    }

    return {
      userInfo,
      handleExitBtn,
      handleUserInfoBtn,
      dialogVisible
    }
  }
})
</script>

<style scoped lang="less">
.info-right {
  display: flex;
  align-items: center;

  .userInfo {
    display: flex;
    flex-direction: column;

    span {
      display: block;
      font-size: 15px;
      margin-bottom: 20px;
    }
  }

  .info-icon span {
    margin: 0 10px;
  }

  .el-dropdown-link {
    display: flex;
    align-items: center;
    margin-left: 10px;

    .name {
      padding-left: 8px;
    }
  }
}
</style>
